rm(list = ls())
library(spatialEco)
library(sp)
library(rgdal)
library(foreign)
library(ggplot2)
library(reshape2)
library(dplyr)
library(RColorBrewer)
library(rgeos)
library(scales)
library(extrafont)
library(gridExtra)
setwd("/Users/linma478/Dropbox (IPL)/SwissGeoAssign/TEX/graphs")


###########################
# Fig 2: Effect estimates #
###########################

nat <- "deepskyblue1"
nat2 <- "deepskyblue3"
eth <- "mediumorchid1"
eth2 <- "mediumorchid3"
lan <- "seagreen3"
lan2 <- "seagreen4"

t = list(theme(axis.text=element_text(size=18), axis.title=element_text(size=18), plot.title=element_text(size=18,face="bold")), 
         theme(plot.margin=grid::unit(c(4,7,0,0), "mm")),  geom_hline(aes(yintercept=0),linetype="dashed"),xlab('Years since arrival'), 
         ylim(-0.008,0.044)) 

## plot point estimates co-national##
    
clist <- c("emp")  
for (i in clist) {
  print(i)
  if (i=="emp") title<-"Change in probability of employment (pp)"
  dat <- read.delim(sprintf("txt_files/%s_lnmatch.txt",i))
  ggplot(dat, aes(y=val, x=X)) + theme_update(plot.title = element_text(hjust = 0.5))
  A <- ggplot(dat, aes(y=val, x=X)) + ylab(title) + t + ggtitle('Nationality')+
  geom_pointrange(aes(ymin=cl, ymax=cu), color=nat2,size=0.8) 
  }



## plot point estimates co-ethnic##

clist <- c("emp")    
for (i in clist) {
  print(i)
  if (i=="emp") title<-"Change in probability of employment (pp)"
  dat <- read.delim(sprintf("txt_files/%s_lnematch.txt",i))
  B <- ggplot(dat, aes(y=val, x=X)) + ylab(title) + xlab('Years since arrival') + t  +
   ggtitle('Ethnicity') + geom_pointrange(aes(ymin=cl, ymax=cu), color=eth2,size=0.8) 
  }

## plot point estimates co-language##

clist <- c("emp")    
for (i in clist) {
  print(i)
  if (i=="emp") title<-"Change in probability of employment (pp)"
  dat <- read.delim(sprintf("txt_files/%s_lnlmatch.txt",i))
  C <- ggplot(dat, aes(y=val, x=X)) + ylab(title) + xlab('Years since arrival')  + t  +
  ggtitle('Language') +  geom_pointrange(aes(ymin=cl, ymax=cu), color=lan2,size=0.8) 
  }




####################################
#### Fig 2: predicted employment ###
####################################

t2 = list(xlab('Years since arrival'),geom_line(size=1.2),geom_point(size=4),geom_ribbon(aes(ymin=cl,ymax=cu),alpha=0.2, linetype=0), 
          theme(plot.margin=grid::unit(c(4,7,0,0), "mm")),geom_hline(aes(yintercept=0),linetype="dashed"), ylim(-0.05,0.45),
          scale_shape_discrete(name="Network members", labels=c("Low (25 pctl)","High (75 pctl)")),
          theme(axis.text=element_text(size=18),axis.title=element_text(size=18),legend.title=element_text(size=14) , 
                legend.text=element_text(size=14), legend.position=c(.8,.2),plot.title=element_text(size=18,face="bold")) )


## plot predicted employment for nationals##
clist <- c("emp")   
for (i in clist) {
  print(i)
if (i=="emp") title<-"Probability of employment (%)"
  dat <- read.delim(sprintf("txt_files/%s_lnmatch_2575.txt",i))
  D <- ggplot(dat, aes(x=num,y=val,color=factor(var), shape=factor(var))) + ylab(title) + ggtitle('Nationality') + t2 +
   scale_color_manual(name="Network members", labels=c("Low (25 pctl)","High (75 pctl)"), values=c(nat, nat2)) 
}

 

## plot predicted employment for ethnicity ##
clist <- c("emp")   
for (i in clist) {
  print(i)
  if (i=="emp") title<-"Probability of employment (%)"
  dat <- read.delim(sprintf("txt_files/%s_lnematch_2575.txt",i))
  E <- ggplot(dat, aes(x=num,y=val,color=factor(var), shape=factor(var))) + ylab(title) + ggtitle('Ethnicity') + t2 +
    scale_color_manual(name="Network members", labels=c("Low (25 pctl)","High (75 pctl)"), values=c(eth, eth2)) 
}

## plot predicted employment for language ##
clist <- c("emp")   
for (i in clist) {
  print(i)
  if (i=="emp") title<-"Probability of employment (%)"
  dat <- read.delim(sprintf("txt_files/%s_lnlmatch_2575.txt",i))
  F <- ggplot(dat, aes(x=num,y=val,color=factor(var), shape=factor(var))) + ylab(title) + ggtitle('Language') + t2 +
    scale_color_manual(name="Network members", labels=c("Low (25 pctl)","High (75 pctl)"), values=c(lan , lan2)) 
}

pdf("results.pdf",width = 20,height=10)
grid.arrange(A,B,C,D,E,F, layout_matrix = rbind(c(1,2,3),c(4,5,6)))
dev.off()

#############################
## Fig 3 and S1 : Bar plot ##
#############################


t3 = list(theme(axis.text=element_text(size=16),axis.title=element_text(size=16),legend.title=element_text(size=12), 
     legend.text=element_text(size=12),legend.position=c(.86,.82)), 
     scale_fill_manual(name= "Based on:",labels=c("Nationality","Ethnicity","Language" ), values=c(nat2, eth2,lan2)) )


dat <- read.delim("txt_files/share_coworkers.txt")
dat$X.1 <- NULL
dat$group2 <- ifelse(dat$group==1,'nat',ifelse((dat$group==2),'eth', ifelse((dat$group==3), 'lan' ,NA)))


title <- c('Co-workers', 'Co-workers \n (firm 10+ employees)','Employed refugees \n in canton', 'Employed refugees \n in sector & canton' )
dat <- data.frame(title, dat)
dat$X <- NULL


ggplot(dat, aes(x=reorder(title,sorder),y=value, fill=group2)) + geom_bar(stat="identity", width=0.5, position=position_dodge(width = 0.8))  + 
  ylab('Share network members (%)') + xlab('') + t3 + theme(axis.text.x=element_text(size=10)) +
 geom_linerange(aes(ymin=lb, ymax=ub), colour="black", size=0.5, position=position_dodge(width = 0.8))
ggsave("share_co-worker.pdf", width = 6, height = 4)


dat <- read.delim("txt_files/num_coworkers.txt")
dat$X.1 <- NULL
dat <- melt(dat, id.vars='X')


ggplot(dat, aes(x=X, y=value, fill=variable)) +
  geom_bar(stat='identity', width=0.5, position=position_dodge(width = 0.8)) +
  ylab('Share of individuals (%)')  + xlab('Number of co-workers') +  t3
ggsave("number_co-worker.pdf", width = 6, height = 4)


##############################
## Fig S2: permutation test ##
##############################

rm(list = ls())
nat <- "deepskyblue1"
nat2 <- "deepskyblue3"
eth <- "mediumorchid1"
eth2 <- "mediumorchid3"
lan <- "seagreen3"
lan2 <- "seagreen4"

t4= list(geom_hline(aes(yintercept=0),linetype="solid", size=0.01) , ylab("Density"),  xlim(0.15,0.36),ylim(-1,100),
         theme(axis.text=element_text(size=18),axis.title=element_text(size=18)))

t <- 

dat <- read.delim("txt_files/permutation_conat.txt")
n  <-  dat[1,4]
n  <-  as.character(n)
A <- ggplot(dat, aes(x = mean)) + geom_density(alpha = .4, fill=nat2)  + t4 +
  geom_vline(aes(xintercept =as.numeric(n)),colour = nat2, linetype ="longdash", size = .8) + xlab('Mean share of co-national co-workers')
print(A)

dat <- read.delim("txt_files/permutation_colan.txt")
l  <-  dat[1,4]
l  <-  as.character(l)
B <-  ggplot(dat, aes(x = mean)) + geom_density(alpha = .4, fill=lan2)  + t4 +
  geom_vline(aes(xintercept = as.numeric(l)),colour = lan2, linetype ="longdash", size = .8) + xlab('Mean share of co-linguistic co-workers')
print(B)

dat <- read.delim("txt_files/permutation_coeth.txt")
e  <-  dat[1,4]
e  <-  as.character(e)
C <- ggplot(dat, aes(x = mean)) + geom_density(alpha = .4, fill=eth2)  + t4 +
  geom_vline(aes(xintercept = as.numeric(e)),colour = eth2, linetype ="longdash", size = .8) + xlab('Mean share of co-ethnic co-workers')
print(C)

pdf("permutation.pdf",width = 20,height=10)
grid.arrange(A,B,C, layout_matrix = rbind(c(1,2,3)))
dev.off()



